package com.knowvoyage.knowvoyageserver.controller;

import com.github.pagehelper.PageInfo;
import com.knowvoyage.knowvoyageserver.pojo.dto.ErrorBookDTO;
import com.knowvoyage.knowvoyageserver.pojo.dto.ErrorRecordDTO;
import com.knowvoyage.knowvoyageserver.pojo.dto.FavoriteDTO;
import com.knowvoyage.knowvoyageserver.pojo.entity.ErrorBook;
import com.knowvoyage.knowvoyageserver.pojo.entity.ErrorRecord;
import com.knowvoyage.knowvoyageserver.pojo.entity.Favorite;
import com.knowvoyage.knowvoyageserver.pojo.vo.ErrorRecordVO;
import com.knowvoyage.knowvoyageserver.pojo.vo.FavoriteVO;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.ErrorBookService;
import com.knowvoyage.knowvoyageserver.service.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "错题类", description = "错题相关接口")
@Slf4j
@RestController
@RequestMapping("/errorbook")
@RequiredArgsConstructor
public class ErrorBookController {
    private final ErrorBookService errorBookService;

    @Operation(summary = "创建错题本", description = "创建错题本接口")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody ErrorBookDTO errorBookDTO){
        ErrorBook errorBook = new ErrorBook();
        BeanUtils.copyProperties(errorBookDTO, errorBook);
        log.info("创建错题本：{}", errorBook);
        return errorBookService.insert(errorBook);
    }
    @Operation(summary = "根据用户ID查询错题本", description = "根据用户ID查询错题本接口")
    @GetMapping("/selectByUserId")
    public Result<List<ErrorBook>> selectByUser(@RequestParam Long userId){
        log.info("根据用户ID查询错题本：{}", userId);
        return errorBookService.selectByUserId(userId);
    }
    @Operation(summary = "删除错题本", description = "删除错题本接口")
    @GetMapping("/delete")
    public Result<Boolean> delete(@RequestParam Long bookId){
        log.info("删除错题本：{}", bookId);
        return errorBookService.delete(bookId);
    }
    @Operation(summary = "更新错题本", description = "更新错题本名称接口")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody ErrorBook errorBook){
        log.info("更新错题本：{}", errorBook);
        return errorBookService.update(errorBook);
    }
    @Operation(summary = "按顺序查询错题本错题", description = "按顺序分页查询bookid错题本所有错题接口")
    @GetMapping("/selectAllRecord")
    public Result<PageInfo<ErrorRecordVO>> selectAllRecord(@RequestParam Long bookId,
                                                           @RequestParam(defaultValue = "1") Integer pageNum,
                                                           @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询bookid错题本所有错题：{}", bookId);
        PageInfo<ErrorRecordVO> errorRecordVOPageInfo = errorBookService.selectAllRecordByBookId(bookId, pageNum, pageSize);
        return Result.success(errorRecordVOPageInfo);
    }

    @Operation(summary = "艾宾浩斯查询错题本错题", description = "查询bookid错题本按遗忘规律需要复习的错题接口")
    @GetMapping("/selectEbbingRecord")
    public Result<PageInfo<ErrorRecordVO>> selectEbbingRecord(@RequestParam Long bookId,
                                                           @RequestParam(defaultValue = "1") Integer pageNum,
                                                           @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询bookid错题本需要复习错题：{}", bookId);
        PageInfo<ErrorRecordVO> errorRecordVOPageInfo = errorBookService.selectEbbingRecordByBookId(bookId, pageNum, pageSize);
        return Result.success(errorRecordVOPageInfo);
    }

    @Operation(summary = "错题本添加错题", description = "添加错题接口")
    @PostMapping("/insertRecord")
    public Result<Boolean> insertRecord(@RequestBody ErrorRecordDTO errorRecordDTO){
        log.info("插入错题本错题：{}", errorRecordDTO);
        ErrorRecord errorRecord = new ErrorRecord();
        BeanUtils.copyProperties(errorRecordDTO, errorRecord);
        return errorBookService.insertRecord(errorRecord);
    }

    @Operation(summary = "删除错题", description = "bookid错题本删除错题接口")
    @GetMapping("/deleteRecord")
    public Result<Boolean> deleteRecord(@RequestParam Long recordId){
        log.info("删除错题：{}", recordId);
        return errorBookService.deleteRecord(recordId);
    }

    @Operation(summary = "批量插入错题", description = "批量插入错题接口，提供错题records对象数组")
    @PostMapping("/batchInsert")
    public Result<Boolean> batchInsert(@RequestBody List<ErrorRecord> records,
                                       Authentication authentication) {
        Long userId = (Long) authentication.getPrincipal();
        // 传递 userId 到 service 层
        return errorBookService.insertRecordsBatch(records, userId);
    }

    @Operation(summary = "批量删除错题", description = "批量删除错题接口，提供recordId数组")
    @PostMapping("/batchDelete")
    public Result<Boolean> batchDelete(@RequestBody List<Long> recordIds,
                                       Authentication authentication) {
        Long userId = (Long) authentication.getPrincipal();
        return errorBookService.deleteRecordsBatch(recordIds, userId);
    }


}
